<?php

class Model{

    public function __construct(){
	
    }
	
	public static function Get($id)
	{
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$dbname = preg_split('/Model/', get_called_class());
		
		$tableinfo = preg_split('/Model/', get_called_class());
		$tablename = strtolower($tableinfo[0]);
		
		$q=$connec->prepare("SELECT * FROM ".$tablename." WHERE id = :id"); 
		$q->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
		$q->execute(array( 'id' => $id ));
		$object = $q->fetch();
		 
		return $object;
	}
	
	public static function GetAll()
	{
		$db = new DbContext();
		$connec = $db->getConnection();
		$connec->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		
		$tableinfo = preg_split('/Model/', get_called_class());
		$tablename = strtolower($tableinfo[0]);
		
		$q=$connec->query("SELECT * FROM ".$tablename); 
		$q->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
		 
		$objects = $q->fetchAll();
		$q->closeCursor();
		 
		return $objects;
	}
	
	public function exists()
	{
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$tableinfo = preg_split('/Model/', get_class($this));
		$tablename = strtolower($tableinfo[0]);
		
		$q=$connec->prepare("SELECT id FROM ".$tablename." WHERE id=:id"); 
		$q->execute(array( 'id' => $this->id ));
		
		$r=$q->fetch();
		return ($r!=null);
	}
	
	public function save()
	{
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$tableinfo = preg_split('/Model/', get_class($this));
		$tablename = strtolower($tableinfo[0]);
		
		$vars = get_class_vars(get_class($this));
		$param = array();
		
		foreach ($vars as $name=>$value)
		{
			if ($name != 'id')
				$param[] = $this->$name;
		}
		
		if ($this->exists())
		{
			$query = "UPDATE ".$tablename." SET ";
			$i=0;
			$vars_length = count($vars);
			foreach ($vars as $name=>$value)
			{
				if ($name != 'id')
				{
					if ($i != $vars_length - 2)
						$query .= '`'.$name.'` = ?, ';
					else
						$query .= '`'.$name.'` = ? WHERE id = ?';
					
					$i++;
				}
			}
			$param[] = $this->id;
		}
		else
		{
			$query = "INSERT INTO ".$tablename." (";
			$values = '';
			$i=0;
			$vars_length = count($vars);
			foreach ($vars as $name=>$value)
			{
				if ($name != 'id')
				{
					if ($i != $vars_length - 1)
					{
						$values .= '?, ';
						$query .= '`'.$name.'`, ';
					}
					else
					{
						$query .= '`'.$name.'`) VALUES(';
						$values .= '?)';
					}
				}
				$i++;
			}
			$query .= $values;
		}

		$q=$connec->prepare($query); 
		$count = $q->execute($param) or die(print_r($q->errorInfo(), true));
	}
	
	public function delete()
	{
		$db = new DbContext();
		$connec = $db->getConnection();
		
		$tableinfo = preg_split('/Model/', get_class($this));
		$tablename = strtolower($tableinfo[0]);
		
		if ($this->exists())
		{
			$query = "DELETE FROM ".$tablename." WHERE id = ?";
			$q = $connec->prepare($query); 
			$count = $q->execute(array($this->id)) or die(print_r($q->errorInfo(), true));
		}
	}
}


?>